package com.andatsoft.laisim.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.entity.Song;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;

public class LibraryAllSongsAdapter extends ArrayAdapter<Song> {

	private List<Song> mSongList;
	private int mSelectedItem = -1;
	private LayoutInflater inflater;
	private Theme mTheme;

	public LibraryAllSongsAdapter(Context context, List<Song> songs,
			LayoutInflater inflater) {
		super(context, 0, songs);
		this.mSongList = songs;
		this.inflater = inflater;
		mTheme = ThemeManager.getSelectedTheme();
	}

	public class ViewHolder {
		TextView tvTitle;
		TextView tvArtist;
		TextView tvAlbum;
		TextView tvDuration;
	}

	@Override
	public int getCount() {
		if (mSongList == null)
			return 0;
		return mSongList.size();
	}

	@SuppressLint("NewApi") @Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder vHolder;
		if (view == null) {
			view = inflater.inflate(R.layout.library_list_songs_item, parent,
					false);
			vHolder = new ViewHolder();
			TextView tvTitle = (TextView) view
					.findViewById(R.id.tv_library_all_songs_title);
			vHolder.tvTitle = tvTitle;
			TextView tvArtist = (TextView) view
					.findViewById(R.id.tv_library_all_songs_artist);
			vHolder.tvArtist = tvArtist;
			TextView tvDur = (TextView) view
					.findViewById(R.id.tv_library_all_songs_duration);
			vHolder.tvDuration = tvDur;
			TextView tvAlbum = (TextView) view
					.findViewById(R.id.tv_library_all_songs_album);
			vHolder.tvAlbum = tvAlbum;

			view.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) view.getTag();
		}

		// Because the view holder is holding the reference of the text
		// view...
		Song s = mSongList.get(position);
		vHolder.tvTitle.setText(s.getTitle());
		vHolder.tvArtist.setText(s.getArtist());
		vHolder.tvAlbum.setText(s.getAlbum());
		vHolder.tvDuration.setText(s.getDurationString());

		// apply theme

		// // highlight view selected
		if (position == this.mSelectedItem) {
			vHolder.tvTitle.setTextSize(mTheme.getMPLSelectedTitleSize());
			vHolder.tvTitle.setTextColor(mTheme.getMPLSelectedTitleColor());
			vHolder.tvArtist.setTextColor(mTheme.getMPLSelectedArtistColor());
			vHolder.tvArtist.setTextSize(mTheme.getMPLSelectedArtistSize());
			vHolder.tvAlbum.setTextColor(mTheme.getMPLSelectedArtistColor());
			vHolder.tvAlbum.setTextSize(mTheme.getMPLSelectedArtistSize());
			vHolder.tvDuration.setTextColor(mTheme.getMPLSelectedArtistColor());
			vHolder.tvDuration.setTextSize(mTheme.getMPLSelectedArtistSize());
		} else {
			vHolder.tvTitle.setTextSize(mTheme.getMPLNormalTitleSize());
			vHolder.tvTitle.setTextColor(mTheme.getMPLNormalTitleColor());
			vHolder.tvArtist.setTextColor(mTheme.getMPLNormalArtistColor());
			vHolder.tvArtist.setTextSize(mTheme.getMPLNormalArtistSize());
			vHolder.tvAlbum.setTextColor(mTheme.getMPLNormalArtistColor());
			vHolder.tvAlbum.setTextSize(mTheme.getMPLNormalArtistSize());
			vHolder.tvDuration.setTextColor(mTheme.getMPLNormalArtistColor());
			vHolder.tvDuration.setTextSize(mTheme.getMPLNormalArtistSize());
		}
		return view;
	}

	public void setSelectedItem(int pos) {
		if (pos > mSongList.size() - 1 || pos < 0)
			pos = -1;
		this.mSelectedItem = pos;
	}
}
